package com.tich.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tich.api.domain.dto.BalanceDeductDTO;
import com.tich.user.domain.entity.User;
import com.tich.user.mapper.UserMapper;
import com.tich.user.service.ClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class ClientServiceImpl implements ClientService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 扣除用户余额
     * @param balanceDeductDTO
     * @return
     * */
    @Transactional
    public Boolean balanceDeduct(BalanceDeductDTO balanceDeductDTO) {
        LambdaUpdateWrapper<User> userLambdaUpdateWrapper = Wrappers.lambdaUpdate(User.class)
                .setSql("balance = balance -" + balanceDeductDTO.getDeduct())
                .eq(User::getId, balanceDeductDTO.getUserId())
                .ge(User::getBalance, balanceDeductDTO.getDeduct());
        return userMapper.update(userLambdaUpdateWrapper) > 0;
    }

    @Override
    public User getById(Long id) {
        return userMapper.selectById(id);
    }
}
